﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;

namespace BLL.Dal
{
    /// <summary>
    /// 数据访问类:RoleDAL
    /// </summary>
    public partial class RoleDAL
    {
        public RoleDAL()
        { }
        #region  BasicMethod

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(string roleCode)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from tbRole");
            strSql.Append(" where roleCode=@roleCode ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@roleCode", OleDbType.VarChar,255)			};
            parameters[0].Value = roleCode;

            return DbHelperOleDb.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add(Model.Role model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into tbRole(");
            strSql.Append("roleCode,roleName,powerCodes,powerNames)");
            strSql.Append(" values (");
            strSql.Append("@roleCode,@roleName,@powerCodes,@powerNames)");
            OleDbParameter[] parameters = {
					new OleDbParameter("@roleCode", OleDbType.VarChar,255),
					new OleDbParameter("@roleName", OleDbType.VarChar,255),
					new OleDbParameter("@powerCodes", OleDbType.VarChar,255),
					new OleDbParameter("@powerNames", OleDbType.VarChar,255)};
            parameters[0].Value = model.roleCode;
            parameters[1].Value = model.roleName;
            parameters[2].Value = model.powerCodes;

            //获取权限名称，逗号分割
            string strPowerNames = GetPowerName(model.powerCodes);

            parameters[3].Value = strPowerNames;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 根据权限标识来获取权限名称
        /// </summary>
        /// <param name="powerCode"></param>
        /// <returns></returns>
        public string GetPowerName(string powerCode)
        {
            if (string.IsNullOrEmpty(powerCode)) return "";
            string strPower = "'" + powerCode.Replace(",", "','") + "'";
            StringBuilder sbTemp = new StringBuilder();
            MenuPowerDAL menuPowerBus = new MenuPowerDAL();
            DataTable dtPower = menuPowerBus.GetList(" powerCode in( " + strPower + ")").Tables[0];
            for (int i = 0; i < dtPower.Rows.Count; i++)
            {
                sbTemp.Append(dtPower.Rows[i]["powerName"] + ",");
            }
            return sbTemp.ToString().TrimEnd(',');
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(Model.Role model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update tbRole set ");
            strSql.Append("roleName=@roleName,");
            strSql.Append("powerCodes=@powerCodes,");
            strSql.Append("powerNames=@powerNames");
            strSql.Append(" where roleCode=@roleCode ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@roleName", OleDbType.VarChar,255),
					new OleDbParameter("@powerCodes", OleDbType.VarChar,255),
					new OleDbParameter("@powerNames", OleDbType.VarChar,255),
					new OleDbParameter("@roleCode", OleDbType.VarChar,255)};
            parameters[0].Value = model.roleName;
            parameters[1].Value = model.powerCodes;
            //获取权限名称，逗号分割
            string strPowerNames = GetPowerName(model.powerCodes);
            parameters[2].Value = strPowerNames;
            parameters[3].Value = model.roleCode;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(string roleCode)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbRole ");
            strSql.Append(" where roleCode=@roleCode ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@roleCode", OleDbType.VarChar,255)			};
            parameters[0].Value = roleCode;

            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        /// 批量删除数据
        /// </summary>
        public bool DeleteList(string roleCodelist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from tbRole ");
            strSql.Append(" where roleCode in (" + roleCodelist + ")  ");
            int rows = DbHelperOleDb.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Role GetModel(string roleCode)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select roleCode,roleName,powerCodes,powerNames from tbRole ");
            strSql.Append(" where roleCode=@roleCode ");
            OleDbParameter[] parameters = {
					new OleDbParameter("@roleCode", OleDbType.VarChar,255)			};
            parameters[0].Value = roleCode;

            Model.Role model = new Model.Role();
            DataSet ds = DbHelperOleDb.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public Model.Role DataRowToModel(DataRow row)
        {
            Model.Role model = new Model.Role();
            if (row != null)
            {
                if (row["roleCode"] != null)
                {
                    model.roleCode = row["roleCode"].ToString();
                }
                if (row["roleName"] != null)
                {
                    model.roleName = row["roleName"].ToString();
                }
                if (row["powerCodes"] != null)
                {
                    model.powerCodes = row["powerCodes"].ToString();
                }
                if (row["powerNames"] != null)
                {
                    model.powerNames = row["powerNames"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select roleCode,roleName,powerCodes,powerNames ");
            strSql.Append(" FROM tbRole ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperOleDb.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM tbRole ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperOleDb.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            int pageSize = endIndex - startIndex;
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * from ( ");


            strSql.Append(" select top ");
            ///页大小
            strSql.Append(pageSize);
            strSql.Append(" * from ( ");

            #region
            strSql.Append(" select top ");
            strSql.Append(endIndex);
            strSql.Append(" * from tbRole ");
            strSql.Append(strWhere);
            strSql.Append("order by createDateTime desc");
            #endregion

            strSql.Append(")");
            strSql.Append("order by createDateTime asc");

            strSql.Append(")");
            strSql.Append("order by createDateTime desc");

            return DbHelperOleDb.Query(strSql.ToString());
        }

        /*
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            OleDbParameter[] parameters = {
                    new OleDbParameter("@tblName", OleDbType.VarChar, 255),
                    new OleDbParameter("@fldName", OleDbType.VarChar, 255),
                    new OleDbParameter("@PageSize", OleDbType.Integer),
                    new OleDbParameter("@PageIndex", OleDbType.Integer),
                    new OleDbParameter("@IsReCount", OleDbType.Boolean),
                    new OleDbParameter("@OrderType", OleDbType.Boolean),
                    new OleDbParameter("@strWhere", OleDbType.VarChar,1000),
                    };
            parameters[0].Value = "tbRole";
            parameters[1].Value = "roleCode";
            parameters[2].Value = PageSize;
            parameters[3].Value = PageIndex;
            parameters[4].Value = 0;
            parameters[5].Value = 0;
            parameters[6].Value = strWhere;	
            return DbHelperOleDb.RunProcedure("UP_GetRecordByPage",parameters,"ds");
        }*/

        #endregion  BasicMethod
        #region  ExtensionMethod

        #endregion  ExtensionMethod
    }
}
